package com.tln.sys.menu.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tln.sys.exception.service.ServiceException;
import com.tln.sys.menu.dao.MenuDao;
import com.tln.sys.menu.model.Menu;
import com.tln.sys.menu.service.MenuService;
import com.xjj.core.modules.orm.Finder;
import com.xjj.core.modules.orm.Page;

@Transactional
@Service("menuService")
public class MenuServiceImpl implements MenuService{
	
	protected Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
	
	private MenuDao menuDao;
	
	@Autowired
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}
	
	public void saveMenu(Menu entity) {
		this.menuDao.save(entity);
	}
	
	public void updateMenu(Menu entity) {
		this.menuDao.update(entity);
	}

	public void deleteMenu(Long id) {
		logger.info("id-- "+id);
		try {
			this.menuDao.delete(id);
			//dao 层的异常就不进行处理。
			/*Menu entity = new Menu();
			//entity.setId(1000L);
			entity.setIcon("aaa");
			entity.setMenuName("bbb");
			entity.setUrl("00000");
			entity.setId(1000L);
			menuDao.save(entity);*/
		} catch (Exception e) {
			throw new ServiceException("不能进行添加操作");
		}
		
	}
	
	public List<Menu> getAllMenuList(){
		return this.menuDao.findAll();
	}

	//要想上百万数据库，就必须分两步走，先获取id 值，再根据id来获取分页的数据，再进行封装
	//1、获取数据库总数、获取id,2、使用in 获取所需要的
	//下一步要加条件进行查询和测试
	@Transactional(readOnly=true)
	public Page<Menu> getAllMenuByHql(Map map, int pageNo, int pageSize) {
		/*try {
			Menu entity = new Menu();
			//entity.setId(1000L);
			entity.setIcon("aaa");
			entity.setMenuName("bbb");
			entity.setUrl("00000");
			//entity.setId(1000L);
			menuDao.save(entity);
		} catch (Exception e) {
			throw new ServiceException("查询数据不能进行添加操作");//异常的处理。
		}
		*/
		String menuName = (String)map.get("menuName");
		Finder f = Finder.create("select bean.id from Menu bean where 1=1");
		f.append(" order by bean.id desc");
		if (StringUtils.isNotEmpty(menuName)) {
			f.append(" and bean.menuName like :menuName").setParam("menuName","%" + menuName + "%" );
		}
		//this.logger.info("hql------ "+f.getOrigHql());
		Page<Menu> page = this.menuDao.findByFinder(f, pageNo, pageSize);
		List<Menu> list = page.getResult();
		//Long date = System.currentTimeMillis();
		//String idString = list.toString();
		String idString = StringUtils.join(list, ",");//这种转换也非常不错
		//logger.info("idTestString:  "+idTestString);
		//Long date1 = System.currentTimeMillis();
		//logger.info("字符串转换时间 --- "+(date1-date));
		//idString = idString.substring(1, idString.length()-1);
		logger.info("idString:  "+idString);
		
		//Long date00 = System.currentTimeMillis();
		
		String hql = "select bean from Menu bean where 1=1";
		if (StringUtils.isNotEmpty(idString)) {
			hql += " and id in ("+idString+")";
		}
		hql += " order by bean.id desc";
		List<Menu> resultList = this.menuDao.find(hql);
		//Long date11 = System.currentTimeMillis();
		//logger.info("in 查询date --- "+(date11-date00));
		page.setResult(resultList);
		return page;
	}

	public Menu getMenu(Long id) {
		Menu menu = this.menuDao.getMenu(id);
		return menu;
	}
	

}
